//
/* HomePanel.java 
 * 	This is the main HomePanel for admin users (could change depending on how we implement the user access control)
 *  
 *  The main HomePanel window is a JPanel
 *  	
 *    Components within the JPanel
 *  	- JTabbedPane with each of the following Panels
 *  	  * CurrentPatientsPanel.java		- Lists current patients in a sortable table. 
 *  											Once a User is selected, a window will pop up with their file
 *  	  * Add/Admit Patients				- Starts the Screening Process or Admits a patient that has been prescreened
 *  	  * Reports (only for admin users) 	- Screen for generating and printing organizational statistics/reports
 *  	  * Administration 				- Add/Remove users
 * 
 * */
	 
	 
	 
	 
	 
/*
Major modifications from Michael Adams 4/16/2014
- Separate non-administrator panel created
- Login Page sub-class implemented
- User Account class tied in
- Integration with current version of panels, should be compatible as of 9:00PM that day

- This is totally a horrible hack, but I've had no luck with anything else.
*/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
 
public class HomePanel extends JPanel{
    
    static JFrame frame;
    //Position changed - Michael Adams, lolNetBeans so kooky.
    public static void main(String[] args) {
	System.out.println("Does this even print?");
	/* Set the Nimbus look and feel */
	try {
	    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
	    if ("Nimbus".equals(info.getName())) {
	    UIManager.setLookAndFeel(info.getClassName());
	    	break;
	       }
	    }
            } catch (Exception e) {
	       // If Nimbus is not available, you can set the GUI to another look and feel.
	    }

	/* Create and display the form */
	SwingUtilities.invokeLater(new Runnable() {
            public void run() {
	    createAndShowGUI();
	    }});
    }
    
    /** Create the GUI and show it. **/
    //Modified, Michael Adams - Login page first
	private static void createAndShowGUI() {
        //Create and set up the window.
        frame = new JFrame("West Slope Recovery - ITRDB");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Add content to the window.
        frame.add(new LoginPage(), BorderLayout.CENTER);
        
        //Display the window.
        frame.setSize(new Dimension(800,600));
        frame.setVisible(true);
	}
    
    private JPanel currentPatientsPanel;
    private JPanel addAdmitPanel;
    private JPanel reportsPanel;
    private JPanel adminPanel;
    private JButton logoutButton;		//will be added as a button with absolute positioning
                 
   /** HomePanel Constructor - Counselor version**/
    public HomePanel() {
                        this.setBackground(Color.WHITE); //sets the main window to white  
                                        
                //TabbedPane and Components of GUI 
                JTabbedPane tabbedPane = new JTabbedPane();
                ImageIcon icon = createImageIcon("images/icon.png");
                 
                currentPatientsPanel = new CurrentClientsPanel(); //CurrentPatientsPanel 
                tabbedPane.addTab("Current Patients", icon, currentPatientsPanel,
                        "List of Current Patients at WSR");             
                      
                tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
                 
                addAdmitPanel = new AddAdmitPanel(); //Charlie Thao changed this to create an object of a class I made
					 addAdmitPanel.setBackground(Color.WHITE); 
                tabbedPane.addTab("Add/Admit Patients", icon, addAdmitPanel,
                        "Begin Screening Process or Admit a Pre-Screened Patient");
                tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
                 
                      
                                
                //tabbedPane font
                        tabbedPane.setFont(new Font("Arial", Font.PLAIN, 14));      
                        
                //logout Button
                        logoutButton = new JButton("Logout");
                        logoutButton.setLayout(null); // allows us to put the button wherever we wish in the window
                        logoutButton.setSize(new Dimension(50,50));
                        //add(logoutButton);
                
                //Add components to this panel (home window using GridBagLayout).
                        //layout
                        //(1 row, 2 columns)                                    column 1                                        column 2        
                        //                                      row 1   | [---------tabbedPane----------] | [logoutframe]  | 
               GridBagConstraints c = new GridBagConstraints();
                    
                    c.anchor = GridBagConstraints.NORTHWEST;
                    c.weightx = 1; //space to allocate to cell [half a cell]
                    c.weighty = 1;
                    c.gridx = 0; //x coordinate
                    c.gridy = 0; //y coordinate
                add(tabbedPane, c); //tabbedPane
                
                c.anchor = GridBagConstraints.FIRST_LINE_START;
                c.gridx = 1;
                c.gridy = 0;
                c.weightx = .1; //space to allocate to cell [half a cell]
                    c.weighty = .1;
                add(logoutButton, c);;
                
                
                
                 
                //The following line enables to use scrolling tabs.
                tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
	}

            //Second Construction - Admin Home Panel
	public HomePanel(int flag) {
                        this.setBackground(Color.WHITE); //sets the main window to white  
                                        
                //TabbedPane and Components of GUI 
                JTabbedPane tabbedPane = new JTabbedPane();
                ImageIcon icon = createImageIcon("images/icon.png");
                 
                currentPatientsPanel = new CurrentClientsPanel(); //CurrentPatientsPanel 
                tabbedPane.addTab("Current Patients", icon, currentPatientsPanel,
                        "List of Current Patients at WSR");             
                      
                tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
                 
                addAdmitPanel = new AddAdmitPanel(); //Charlie Thao changed this to create an object of a class I made
					 addAdmitPanel.setBackground(Color.WHITE); 
                tabbedPane.addTab("Add/Admit Patients", icon, addAdmitPanel,
                        "Begin Screening Process or Admit a Pre-Screened Patient");
                tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
                 
                JPanel reportsPanel = new JPanel(); 
                tabbedPane.addTab("Reports", icon, reportsPanel,
                        "Exporting WSR Organizational Reports and Statistics");
                tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
                 
                JPanel adminPanel = new JPanel(); 
                tabbedPane.addTab("Administration", icon, adminPanel,
                        "Creating Employees / Deleting Employees / Viewing System Information and Logs");
                tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);                   
                                
                //tabbedPane font
                        tabbedPane.setFont(new Font("Arial", Font.PLAIN, 14));      
                        
                //logout Button
                        logoutButton = new JButton("Logout");
                        logoutButton.setLayout(null); // allows us to put the button wherever we wish in the window
                        logoutButton.setSize(new Dimension(50,50));
                        //add(logoutButton);
                
                //Add components to this panel (home window using GridBagLayout).
                        //layout
                        //(1 row, 2 columns)                                    column 1                                        column 2        
                        //                                      row 1   | [---------tabbedPane----------] | [logoutframe]  | 
               GridBagConstraints c = new GridBagConstraints();
                    
                    c.anchor = GridBagConstraints.NORTHWEST;
                    c.weightx = 1; //space to allocate to cell [half a cell]
                    c.weighty = 1;
                    c.gridx = 0; //x coordinate
                    c.gridy = 0; //y coordinate
                add(tabbedPane, c); //tabbedPane
                
                c.anchor = GridBagConstraints.FIRST_LINE_START;
                c.gridx = 1;
                c.gridy = 0;
                c.weightx = .1; //space to allocate to cell [half a cell]
                    c.weighty = .1;
                add(logoutButton, c);;
                
                
                
                 
                //The following line enables to use scrolling tabs.
                tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
	}
	
	
	/** Returns an ImageIcon, or null if the path was invalid. */
	protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = HomePanel.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
	}
        
    //Creates the login page, controls itself and passes control elsewhere on successful login
    public static class LoginPage extends javax.swing.JPanel{

    /**
     * Creates new form LoginPage
     */
    public LoginPage() {

        initComponents();
    }

    //Voodoo courtesy of NetBeans. Password textfield needs changing to /actual/
    //password field
    private void initComponents() {

        LogInButton = new javax.swing.JButton();
        ExitButton = new javax.swing.JButton();
        UNLab = new javax.swing.JLabel();
        PWLab = new javax.swing.JLabel();
        user = new javax.swing.JTextField();
        pass = new javax.swing.JTextField();

        setBackground(new java.awt.Color(255, 255, 255));

        LogInButton.setText("Log In");
        LogInButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LogInButton(evt);
            }
        });

        ExitButton.setText("Exit");
        ExitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitButton(evt);
            }
        });

        UNLab.setText("Username");

        PWLab.setText("Password");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ExitButton)
                        .addGap(44, 44, 44)
                        .addComponent(LogInButton))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(UNLab)
                        .addGap(18, 18, 18)
                        .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(PWLab)
                        .addGap(18, 18, 18)
                        .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(UNLab)
                    .addComponent(user, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(PWLab)
                    .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ExitButton)
                    .addComponent(LogInButton))
                .addContainerGap(30, Short.MAX_VALUE))
        );
    }// </editor-fold>                        

    //Gets a login button if the validation comes back positive. Displays scolding
    //message otherwise
    private void LogInButton(java.awt.event.ActionEvent evt) {
        UserAccount acctman = new UserAccount();
        int checker = acctman.Validate(user.getText(), pass.getText());
        if(checker == 0)
        {
          frame.getContentPane().removeAll();
          frame.getContentPane().add(new HomePanel());//Adding to content pane, not to Frame
          frame.repaint();
          frame.printAll(getGraphics());//Extort print all content
        }
        else if(checker == 1)
        {
            frame.getContentPane().removeAll();
            frame.getContentPane().add(new HomePanel(1));//Adding to content pane, not to Frame
            frame.repaint();
            frame.printAll(getGraphics());//Extort print all content
        }
        JOptionPane pane = new JOptionPane();

        JOptionPane.showMessageDialog(pane, "Username or password invalid.");
    }                                           

    //Self explanatory
    private void ExitButton(java.awt.event.ActionEvent evt) {                                           
        System.exit(0);
    }                                          


    // Variables declaration - do not modify                     
    private javax.swing.JButton ExitButton;
    private javax.swing.JButton LogInButton;
    private javax.swing.JLabel PWLab;
    private javax.swing.JLabel UNLab;
    private javax.swing.JTextField pass;
    private javax.swing.JTextField user;
    // End of variables declaration                   
    }
}